package com.qj.yantaishan.controller;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qj.yantaishan.common.annotation.Log;
import com.qj.yantaishan.common.enums.BusinessType;
import com.qj.yantaishan.domain.ProductImage;
import com.qj.yantaishan.service.IProductImageService;
import com.qj.yantaishan.common.core.controller.BaseController;
import com.qj.yantaishan.common.core.domain.AjaxResult;
import com.qj.yantaishan.common.utils.poi.ExcelUtil;
import com.qj.yantaishan.common.core.page.TableDataInfo;

/**
 * 商品图片Controller
 * 
 * @author qihe
 * @date 2022-07-22
 */
@RestController
@RequestMapping("/yantaishan/ProductImage")
public class ProductImageController extends BaseController
{
    private String prefix = "yantaishan/ProductImage";

    @Autowired
    private IProductImageService productImageService;

    @RequiresPermissions("yantaishan:image:view")
    @GetMapping()
    public String image()
    {
        return prefix + "/image";
    }

    /**
     * 查询商品图片列表
     */
    @RequiresPermissions("yantaishan:image:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductImage productImage)
    {
        startPage();
        List<ProductImage> list = productImageService.selectProductImageList(productImage);
        return getDataTable(list);
    }

    /**
     * 导出商品图片列表
     */
    @RequiresPermissions("yantaishan:image:export")
    @Log(title = "商品图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductImage productImage)
    {
        List<ProductImage> list = productImageService.selectProductImageList(productImage);
        ExcelUtil<ProductImage> util = new ExcelUtil<ProductImage>(ProductImage.class);
        return util.exportExcel(list, "商品图片数据");
    }

    /**
     * 新增商品图片
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品图片
     */
    @RequiresPermissions("yantaishan:image:add")
    @Log(title = "商品图片", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductImage productImage)
    {
        return toAjax(productImageService.insertProductImage(productImage));
    }

    /**
     * 修改商品图片
     */
    @RequiresPermissions("yantaishan:image:edit")
    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId") Integer productId, ModelMap mmap)
    {
        ProductImage productImage = productImageService.selectProductImageByProductId(productId);
        mmap.put("productImage", productImage);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品图片
     */
    @RequiresPermissions("yantaishan:image:edit")
    @Log(title = "商品图片", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductImage productImage)
    {
        return toAjax(productImageService.updateProductImage(productImage));
    }

    /**
     * 删除商品图片
     */
    @RequiresPermissions("yantaishan:image:remove")
    @Log(title = "商品图片", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(productImageService.deleteProductImageByProductIds(ids));
    }
}
